//
//  CTReleasedVC.m
//  digitalCurrency
//
//  Created by 默认账户 on 2019/6/6.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTReleasedVC.h"
#import "CTReleasedHeaderView.h"
#import "CEPointListController.h"
#import "CEPointInstructionModel.h"
#import "MineNetManager.h"
#import "ShouYiCell.h"
@interface CTReleasedVC ()
@property (nonatomic, strong) NSMutableArray *pointInsList;
@property (nonatomic, strong) LYEmptyView *emptyView;
@property (nonatomic, copy) NSString *referTeamAsset;
@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UILabel *ziChanLB;
@end

@implementation CTReleasedVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _page = 1;
        _pointInsList = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewTitle.text = LocalizationKey(@"节点收益");
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorColor  = [UIColor clearColor];
    [self.tableView registerClass:[ShouYiCell class] forCellReuseIdentifier:@"qgcell"];
    self.tableView.rowHeight = 60;
    [self headRefreshWithScrollerView:self.tableView];
    [self footRefreshWithScrollerView:self.tableView];
    
    [self getReleaseIncomeList];
    
    [self setHeadView];
}

- (void)setHeadView{
    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW,  80)];
    self.tableView.tableHeaderView = self.headView;
    self.tableView.tableFooterView = [UIView new];
    
    UIView *bgView = [[UIView alloc]init];
    [self.headView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.top.equalTo(13);
        make.right.equalTo(-20);
        make.height.equalTo(60);
    }];
    bgView.backgroundColor = tMainColor;
    bgView.layer.cornerRadius = 5.0;
    
    UILabel *zongZiChanLB = [[UILabel alloc]init];
    [bgView addSubview:zongZiChanLB];
    [zongZiChanLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.top.equalTo(16);
    }];
    zongZiChanLB.textColor = [UIColor whiteColor];
    zongZiChanLB.font = kFont(14);
    zongZiChanLB.text = LocalizationKey(@"总资产");
    
    self.ziChanLB = [[UILabel alloc]init];
    [bgView addSubview:self.ziChanLB];
    [self.ziChanLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.bottom.equalTo(-13);
    }];
    self.ziChanLB.textColor = [UIColor whiteColor];
    self.ziChanLB.font = kFont(12);
    self.ziChanLB.text = @"$00.00";
    
    UIImageView *rightImage = [[UIImageView alloc]init];
    [bgView addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(-20);
        make.centerY.equalTo(0);
    }];
    rightImage.image = ImageName(@"finance_left");
}
    
- (LYEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [LYEmptyView emptyViewWithImageStr:@"emptyData" titleStr:[[ChangeLanguage bundle] localizedStringForKey:@"NoData" value:nil table:@"English"]];
        _emptyView.titleLabTextColor = tThreeColor;
        _emptyView.titleLabFont = [UIFont systemFontOfSize:19];
        _emptyView.contentViewY = SCREEN_HEIGHT/2.0-100;
    }
    return _emptyView;
}
    
- (void)getReleaseIncomeList
 {
     NSDictionary *dic = @{@"pageNum":@(_page),@"pageSize":@"20"};
     [MineNetManager getReleaseIncomeList:dic CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            [self.pointInsList removeAllObjects];

            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            self.ziChanLB.text = [NSString stringWithFormat:@"$%@",[ToolUtil judgeStringForDecimalPlaces:[NSString stringWithFormat:@"%@",resPonseObj[@"data"][@"totalIncome"]] point:4]];

            NSArray *data = resPonseObj[@"data"][@"list"];

            if (codeInt == 0) {
                if (data.count) {
                    for (NSDictionary *subdic in data) {
                        [self.pointInsList addObject:subdic];
                    }
                }
                else {
                    if (_page != 1) {
                        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                    }
                }
                self.tableView.ly_emptyView = self.emptyView;
                [self.tableView reloadData];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }

        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
     
     [self.tableView reloadData];

}

//MARK:--下拉刷新
- (void)refreshHeaderAction {
    _page = 1;
    [self getReleaseIncomeList];
}
- (void)refreshFooterAction {
    _page++;
    [self getReleaseIncomeList];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.pointInsList.count;
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShouYiCell *cell = [ShouYiCell createTableView:tableView];
    NSDictionary *dataDic = self.pointInsList[indexPath.row];
        
    cell.bgView.frame = CGRectMake(0, 0, kWindowW, 60);
    cell.bgView.layer.cornerRadius = 0.01;
    cell.bgView.layer.shadowColor = [UIColor whiteColor].CGColor;
    
    [cell.coinName mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.centerX.mas_equalTo(30);
    }];
    [cell.coinNumber mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-15);
    }];
    [cell.createTime mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
    }];
    
    if ([[NSString stringWithFormat:@"%@",dataDic[@"status"]] isEqualToString:@"0"]) {
        cell.coinName.text = LocalizationKey(@"直推收益");
    }
    else{
        cell.coinName.text = LocalizationKey(@"社区收益");
    }
    
    cell.createTime.text = [NSString stringWithFormat:@"%@",dataDic[@"createTime"]];
    cell.coinNumber.text = [NSString stringWithFormat:@"+%@ USDT",dataDic[@"value"]];

    
    if (indexPath.row%2!=0) {
        cell.bgView.backgroundColor = [[UIColor colorWithHEX:mainBlackColor] colorWithAlphaComponent:0.5];
    }
    else {
        cell.bgView.backgroundColor = [UIColor whiteColor];
    }
    
    return cell;
}


@end
